# -*- coding: UTF-8 -*-
import socket


def test_port(host, port) -> bool:
    """测试端口是否可连接

    Args:
        host (str): 主机地址
        port (str): 主机端口

    Returns:
        bool: 是否可连接
    """
    try:
        # 创建一个TCP套接字
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # 设置连接超时时间为1秒
        sock.settimeout(1)
        # 尝试连接到指定的主机和端口
        result = sock.connect_ex((host, int(port)))
        if result == 0:
            return True
        else:
            return False
    except socket.error as e:
        print(f"发生错误：{e}")
    finally:
        sock.close()
